<?php
$this->rmenu = array(
	array('label'=>'Add member','url'=>array('company_members/create','company_id'=>$model->id),'linkOptions'=>array('id'=>'menu_member')),
	array('label'=>'Add address','url'=>array('company_address/create','company_id'=>$model->id),'linkOptions'=>array('id'=>'menu_address')),
	array('label'=>'Merge with other company', 'url'=>array('merging','id'=>$model->id),'linkOptions'=>array('id'=>'menu_merge')),
);
$this->menu=array(
	array('label'=>'List company', 'url'=>array('list')),
	array('label'=>'Create company', 'url'=>array('create'),'linkOptions'=>array('id'=>'create_company')),
	array('label'=>'Manage company', 'url'=>array('update','id'=>$model->id),'linkOptions'=>array('id'=>'menu_manage')),
	array('label'=>'Delete company', 'url'=>array('delete','id'=>$model->id),'linkOptions'=>array('id'=>'menu_delete')),
);
?>

<h2>Viewing company <?php echo $model->cname; ?>'s information</h2>
<div class="view ui-corner-all">
	<b>Address</b>
	<hr />
	<div class="row">
	<?php $this->forward('company_address/viewall/company_id/'.$model->id,false); ?>
	</div>
	<br />
	
	<b>Member / Employee</b>
	<hr />
	<div class="row" id="member">
	<?php $this->forward('company_members/viewall/company_id/'.$model->id,false); ?>
	</div>
	<br />
	
	<b>Company's supplying cargo transaction history</b>
	<hr />
	<div class="row">
	<?php 
	$this->widget('zii.widgets.grid.CGridView', array(
		'id'=>'company-selling-history',
		'dataProvider'=>$model->supplier_history(),
		'columns'=>array(
			'in_date:date:Arrival date',
			'consignee.cname:text:Consignee',
			'export.export.etd:date:Export date',
			array(
				'name'=>'Package',
				'value'=>'$data->dimension->qty.$data->dimension->qty_type."/".$data->dimension->wt.$data->dimension->wt_type."/".$data->dimension->m3.$data->dimension->m3_type',
			),			
			array(
				'class'=>'CButtonColumn',
				'template'=>'{view} {update}',
				'updateButtonUrl'=>'CHtml::normalizeUrl(array("/cargo/update","id"=>$data->id))',
				'viewButtonUrl'=>'CHtml::normalizeUrl(array("/cargo/view","id"=>$data->id))',
			),
		),
	)); 
	?>
	</div>
	<br />
	<b>Company's buying cargo transaction history</b>
	<hr />
	<div class="row">
	<?php 
	$this->widget('zii.widgets.grid.CGridView', array(
		'id'=>'company-buying-history',
		'dataProvider'=>$model->buyer_history(),
		'columns'=>array(
			'in_date:date:Arrival date',
                        'marking:text:Marking',
			'shipper.cname:text:Supplier',
			'export.export.etd:date:Export date',
			array(
				'name'=>'Package',
				'value'=>'$data->dimension->qty.$data->dimension->qty_type."/".$data->dimension->wt.$data->dimension->wt_type."/".$data->dimension->m3.$data->dimension->m3_type',
			),			
			array(
				'class'=>'CButtonColumn',
				'template'=>'{view} {update}',
				'updateButtonUrl'=>'CHtml::normalizeUrl(array("/cargo/update","id"=>$data->id))',
				'viewButtonUrl'=>'CHtml::normalizeUrl(array("/cargo/view","id"=>$data->id))',
			),
		),
	)); 
	?>
	</div>
	<br />
</div>
<?php 
$cs = Yii::app()->getClientScript();
if(Yii::app()->request->enableCsrfValidation)
{
	$csrfTokenName = Yii::app()->request->csrfTokenName;
	$csrfToken = Yii::app()->request->csrfToken;
	$csrf = "\n\t\tdata:{ '$csrfTokenName':'$csrfToken' },";
}
else
	$csrf='';
$ListUrl = CHtml::normalizeUrl(array('company/list'));
$mergeUrl = CHtml::normalizeUrl(array('merging','id'=>$model->id));
$memberUrl = CHtml::normalizeUrl(array('company_members/viewall','company_id'=>$model->id));
$addressUrl = CHtml::normalizeUrl(array('company_address/viewall','company_id'=>$model->id));
$script =<<<EOD
$('#menu_merge').click(function(){
	var dialogButton = {
		Merge:function(){
			$(this).find('.buttons').find('#merge').click();
		}
	};
	$.simplelogdialog({url:$(this).attr('href')},{width:400},dialogButton);
	return false;
});
$('#ajaxPortlet li a#menu_member').click(function(){
	var dialogButton = {
		Add:function(){
			var constructor = $(this);
			$.company_members_form={
				afterSubmit:function(){
					$('#member').subcontent({url:"$memberUrl",show:{method:'html'}})
					$(constructor).dialog('close');
				}
			};
			$(this).find('form').find('[type=submit]').click();
		}
	};
	$.simplelogdialog({url:$(this).attr('href')},{width:600},dialogButton);
	return false;
});
$('#ajaxPortlet li a#menu_delete').click(function(){
	if(!confirm("Are you to delete this cargo?"))
		return false;
	$.simplelogdialog.Loading.Open('Deleting....');
	$.simplelogajax.ajax({
		$csrf
		url:$(this).attr('href'),
		success:function(){
			$.simplelogdialog.Loading.Close();
			window.location = "$ListUrl";
		},
		complete:function(){
			$.simplelogdialog.Loading.Close();
		}
	});
	return false;
});
$('#ajaxPortlet li a#menu_address').click(function(){
	var dialogButton = {
		Add:function(){
			var constructor = $(this);
			$.company_address_form={
				afterSubmit:function(){
					$('#address').subcontent({url:"$addressUrl",show:{method:'html'}})
					$(constructor).dialog('close');
				}
			};
			$(this).find('form').find('[type=submit]').click();
		}
	};
	$.simplelogdialog({url:$(this).attr('href')},{width:600},dialogButton);
	return false;
});
$('#create_company').click(function(){
	var dialogButton = {
		Create:function(){
			$(this).find('form').find('[type=submit]').click();
		}
	};
	$.simplelogdialog({url:$(this).attr('href')},{width:400},dialogButton);
	return false;
});
$('#menu_manage').click(function(){
	var dialogButton = {
		Create:function(){
			$(this).find('form').find('[type=submit]').click();
		}
	};
	$.simplelogdialog({url:$(this).attr('href')},{width:400},dialogButton);
	return false;
});
EOD;
$cs->registerScript('cargo#view',$script);
?>